from typing import Optional, Any
from application.model.PostModel import PostModel
from application.util.MysqlUtil import mysql_session


class PostMapper:

    @classmethod
    def count(cls) -> int:
        """
        获取帖子总数
        :return: 帖子总数
        """
        result: int = mysql_session.query(PostModel).count()
        return result

    @classmethod
    def info_list(cls, offset: int, page_size: int) -> Any:
        """
        获取所有帖子数据（降序）
        :param offset: 偏移量
        :param page_size: 每页多少条
        :return: 帖子数据列表
        """
        result: Any = mysql_session.query(PostModel).order_by(PostModel.create_timestamp.desc())\
            .limit(page_size).offset(offset).all()
        return result

    @classmethod
    def insert(cls, post_model: PostModel) -> bool:
        """
        插入帖子数据
        :param post_model: 帖子模型
        :return:
        """
        try:
            mysql_session.add(instance=post_model)
            mysql_session.commit()
            return True
        except Exception:
            mysql_session.rollback()
            return False

    @classmethod
    def get_info_by_user_id(cls, user_id: str) -> Optional[PostModel]:
        """
        根据用户ID获取帖子数据
        :param user_id: 用户ID
        :return: 帖子数据
        """
        post: Optional[PostModel] = mysql_session.query(PostModel).filter_by(user_id=user_id).first()
        result: Optional[PostModel] = post if post else None
        return result

    @classmethod
    def get_info_by_id(cls, post_id: str) -> Optional[PostModel]:
        """
        根据帖子ID获取帖子数据
        :param post_id: 帖子ID
        :return: 帖子数据
        """
        post: Optional[PostModel] = mysql_session.query(PostModel).filter_by(id=post_id).first()
        result: Optional[PostModel] = post if post else None
        return result

    @classmethod
    def delete(cls, post_model: PostModel) -> bool:
        """
        删除帖子
        :param post_model: 帖子模型
        :return:
        """
        try:
            mysql_session.delete(instance=post_model)
            mysql_session.commit()
            return True
        except Exception:
            mysql_session.rollback()
            return False

    @classmethod
    def update(cls, post_dict: dict) -> bool:
        """
        更新帖子数据
        :param post_dict: 帖子字典
        :return:
        """
        try:
            mysql_session.query(PostModel).filter_by(id=post_dict.get("id")).update(post_dict)
            mysql_session.commit()
            return True
        except Exception:
            from traceback import print_exc
            print_exc()
            mysql_session.rollback()
            return False

    @classmethod
    def get_info_by_image_id(cls, image_id: str) -> Optional[PostModel]:
        """
        根据图片ID获取帖子数据（占时没用）
        :param image_id: 图片ID
        :return: 帖子数据
        """
        post: Optional[PostModel] = mysql_session.query(PostModel).filter(
            PostModel.image_ids.like("%" + image_id + "%")).first()
        result: Optional[PostModel] = post if post else None
        return result
